package base

import (
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/gobase/getfrom"
)

type ConfValue struct {
	s string
}

func (this *ConfValue) Set(v string) bool {
	if this.s != v {
		this.s = v
		return true
	}
	return false
}

func (this *ConfValue) RawString() string {
	return this.s
}

func (this *ConfValue) String() string {
	raw := gobase.ReplacePlaceholder(this.s, "$rt(", ")$", func(key string) (v string, ok bool) {
		return gobase.ParseSimpleExpWithFunc(key, gobase.TryGetFactoryTokenValue)
	})
	return raw
}

func (this *ConfValue) AsInt(def int) int {
	return gobase.StrToIntDef(this.String(), def)
}

func (this *ConfValue) AsFloat64(def float64) float64 {
	return gobase.StrToFloat64Def(this.String(), def)
}

func (this *ConfValue) GetFrom() ([]byte, error) {
	return getfrom.GetFromS(this.s)
}

func (this *ConfValue) Valid() bool {
	return len(this.s) > 0
}

func ConfValueFrom(s string) ConfValue {
	return ConfValue{s: s}
}
